package com.star.controller.common;

import com.star.constant.MessageConstant;
import com.star.constant.OssConstant;
import com.star.result.Result;
import com.star.utils.AliOssUtil;
import com.star.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.Random;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/common")
@Slf4j
// Swagger接口文档
@Api(tags = "通用接口")
public class CommonController {
    @Autowired
    private AliOssUtil aliOssUtil;


    @PostMapping("/upload")
    @ApiOperation("文件上传")
    public Result<String> upload (MultipartFile file) {
        log.info("文件上传{}", file);
        try {
            String originalFilename = file.getOriginalFilename();
            // 截取后缀名
            String suffix = originalFilename.substring(originalFilename.lastIndexOf("."));
            String newFileName = OssConstant.OSS_PATH + UUID.randomUUID().toString() + suffix;

            String fliePath = aliOssUtil.upload(file.getBytes(), newFileName);
            return Result.success(fliePath);
        } catch (IOException e) {
            log.error("文件上传失败{}", e.getMessage());
        }
        return Result.error(MessageConstant.UPLOAD_FAILED);
    }

}
